package min.diary.client.common.ui;

import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class TagEditor extends Composite {
	private VerticalPanel panel;
	
	private FlowPanel flowPanel;
	private SuggestBox suggestBox;

	private KeyPressHandler enterKeyHandler;
	private HandlerRegistration enterKeyHandlerRegistration;
	
	public TagEditor() {
		enterKeyHandlerRegistration = null;
		
		flowPanel = new FlowPanel();
		suggestBox = new SuggestBox();
		
		panel = new VerticalPanel();
		panel.add(flowPanel);
		panel.add(suggestBox);
		
		initWidget(panel);
		
		enterKeyHandler = new KeyPressHandler() {
			
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getCharCode() == KeyCodes.KEY_ENTER) {
					flowPanel.add(new Label(suggestBox.getText()));
				}
			}
		};
	}

	@Override
	protected void onAttach() {
		enterKeyHandlerRegistration = suggestBox.addKeyPressHandler(enterKeyHandler);
	}

	@Override
	protected void onDetach() {
		if (enterKeyHandlerRegistration != null) {
			enterKeyHandlerRegistration.removeHandler();
		}
	}
	
	
}
